package com.jilefojing.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.data.enums.ResponseEnum;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.ProgressStatusForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.DCategory;
import com.jilefojing.beans.pojos.EVideo;
import com.jilefojing.beans.pojos.RCategorySource;
import com.jilefojing.beans.pojos.RSourceCorrelation;
import com.jilefojing.beans.pojos.RTagSource;
import com.jilefojing.beans.pojos.Tag;
import com.jilefojing.beans.pojos.enhanced.EnhancedDCategory;
import com.jilefojing.beans.pojos.enhanced.EnhancedRCategorySource;
import com.jilefojing.beans.pojos.enhanced.EnhancedRSourceCorrelation;
import com.jilefojing.beans.pojos.enhanced.EnhancedRTagSource;
import com.jilefojing.beans.pojos.enhanced.EnhancedTag;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.query.RCategorySourceQuery;
import com.jilefojing.beans.query.RSourceCorrelationQuery;
import com.jilefojing.beans.query.RTagSourceQuery;
import com.jilefojing.beans.vo.Order;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.beans.vo.Source;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IDCategoryDao;
import com.jilefojing.dao.IEVideoDao;
import com.jilefojing.dao.IRCategorySourceDao;
import com.jilefojing.dao.IRTagSourceDao;
import com.jilefojing.dao.ISourceDao;
import com.jilefojing.dao.ITagDao;
import com.jilefojing.service.IEVideoService;
import com.jilefojing.service.IRCategorySourceService;
import com.jilefojing.service.IRSourceCorrelationService;
import com.jilefojing.service.IRTagSourceService;
import com.jilefojing.service.ISourceService;
import com.jilefojing.service.ITagService;
import com.jilefojing.util.CombinationUtil;
import com.jilefojing.util.Detect;
import com.jilefojing.util.QiniuUtil;

@Service
public class SourceService implements ISourceService {

	private static final Logger _LOGGER = LoggerFactory.getLogger(SourceService.class);

	@Autowired
	private IEVideoService eVideoService;
	@Autowired
	private IEVideoDao eVideoDao;
	@Autowired
	private IRSourceCorrelationService rSourceCorrelationService;
	@Autowired
	private IRTagSourceService rTagSourceService;
	@Autowired
	private IRCategorySourceService rCategorySourceService;
	@Autowired
	private IDCategoryDao dCategoryDao;
	@Autowired
	private IRCategorySourceDao rCategorySourceDao;
	@Autowired
	private ITagService tagService;
	@Autowired
	private IRTagSourceDao rTagSourceDao;
	@Autowired
	private ITagDao tagDao;
	@Autowired
	private ISourceDao sourceDao;

	@Autowired
	private ApplicationContext applicationContext;

	@Override
	public void checkSource(ItemTypeEnumForSource itemType, long itemId, UserContext userContext) throws UserContextException {
		if (itemType == ItemTypeEnumForSource.VIDEO) {
			EVideo eVideo = eVideoDao.getById(itemId);
			if (eVideo == null) {
				throw new UserContextException(ResponseEnum.SOURCE_NOT_EXIST);
			} else if (eVideo.getStatus() != StatusEnumForPublish.ALREADY_RELEASED.getCode()) {
				throw new UserContextException(ResponseEnum.SOURCE_INVALID);
			}
		}
		// else TODO 继续增加类型

	}

	@Override
	public Pagination<? extends BaseEnhanced> getPaginationForCorrelation(ItemTypeEnumForSource itemType, long id, List<ItemTypeEnumForSource> relationTypes,
			Pagination<? extends BaseEnhanced> pagination, UserContext userContext) {
		if (!Detect.notEmpty(pagination.getItems())) {
			pagination.addOrder(new Order(RSourceCorrelation.COLUMN_SORT, Order.ORDER_DESC));
		}
		RSourceCorrelationQuery rSourceCorrelationQuery = new RSourceCorrelationQuery();
		rSourceCorrelationQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));
		rSourceCorrelationQuery.setItemIds(Arrays.asList(new Long[] { id }));
		rSourceCorrelationQuery.setRelationTypes(relationTypes);

		@SuppressWarnings("unchecked")
		Pagination<EnhancedRSourceCorrelation> enhancedRSourceCorrelationPagination = rSourceCorrelationService.getPagination(rSourceCorrelationQuery, pagination.copy(), userContext);

		pagination.setCount(enhancedRSourceCorrelationPagination.getCount());
		if (!Detect.notEmpty(enhancedRSourceCorrelationPagination.getItems())) {
			return pagination;
		}

		rSourceCorrelationService.appendEnhancedRelationItem2EnhancedRSourceCorrelations(enhancedRSourceCorrelationPagination.getItems(), userContext);

		List enhancedRelationItems = new ArrayList();
		for (EnhancedRSourceCorrelation _EnhancedRSourceCorrelation : enhancedRSourceCorrelationPagination.getItems()) {
			if (_EnhancedRSourceCorrelation.getEnhancedRelationItem() != null) {
				enhancedRelationItems.add(_EnhancedRSourceCorrelation.getEnhancedRelationItem());
			}
		}

		pagination.setItems(enhancedRelationItems);
		return pagination;
	}

	@Override
	public Pagination<? extends BaseEnhanced> getPaginationByTagIds(List<Long> tagIds, List<ItemTypeEnumForSource> returnItemTypes, List<StatusEnumForPublish> statuses,
			Pagination<? extends BaseEnhanced> pagination, UserContext userContext) {
		RTagSourceQuery rTagSourceQuery = new RTagSourceQuery();
		rTagSourceQuery.setTagIds(tagIds);
		rTagSourceQuery.setItemTypes(returnItemTypes);
		rTagSourceQuery.setStatuses(statuses);

		Pagination<EnhancedRTagSource> enhancedRTagSourcePagination = rTagSourceService.getPagination(rTagSourceQuery, pagination.copy(), userContext);
		pagination.setCount(enhancedRTagSourcePagination.getCount());
		if (!Detect.notEmpty(enhancedRTagSourcePagination.getItems())) {
			return pagination;
		}

		rTagSourceService.appendEnhancedItem2EnhancedRTagSources(enhancedRTagSourcePagination.getItems(), userContext);

		List enhancedItems = new ArrayList();
		for (EnhancedRTagSource _EnhancedRTagSource : enhancedRTagSourcePagination.getItems()) {
			if (_EnhancedRTagSource.getEnhancedItem() != null) {
				enhancedItems.add(_EnhancedRTagSource.getEnhancedItem());
			}
		}

		pagination.setItems(enhancedItems);
		return pagination;
	}

	@Override
	public Pagination<? extends BaseEnhanced> getPaginationByCategoryIdsForActive(List<Long> categoryIds, List<ItemTypeEnumForSource> returnItemTypes, List<StatusEnumForPublish> statuses,
			List<ProgressStatusForSource> progressStatuses, Pagination<? extends BaseEnhanced> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<EnhancedRCategorySource> enhancedRCategorySourcePagination = rCategorySourceService.getPaginationByCategoryIdsForIntersection(categoryIds, statuses, returnItemTypes,
				progressStatuses, pagination.copy(), userContext);

		pagination.setCount(enhancedRCategorySourcePagination.getCount());
		if (!Detect.notEmpty(enhancedRCategorySourcePagination.getItems())) {
			return pagination;
		}

		rCategorySourceService.appendEnhancedItem(enhancedRCategorySourcePagination.getItems(), userContext);

		List enhancedItems = new ArrayList();
		for (EnhancedRCategorySource _EnhancedRCategorySource : enhancedRCategorySourcePagination.getItems()) {
			if (_EnhancedRCategorySource.getEnhancedItem() != null) {
				enhancedItems.add(_EnhancedRCategorySource.getEnhancedItem());
			}
		}

		pagination.setItems(enhancedItems);
		return pagination;
	}

	@Override
	public BaseEnhanced getEnhancedItem(ItemTypeEnumForSource itemType, long itemId, UserContext userContext) {
		List<BaseEnhanced> enhanceds = this.getEnhancedItems(itemType, Arrays.asList(new Long[] { itemId }), userContext);
		if (!Detect.notEmpty(enhanceds)) {
			return null;
		}
		return enhanceds.get(0);
	}

	@Override
	public List<BaseEnhanced> getEnhancedItems(ItemTypeEnumForSource itemType, List<Long> itemIds, UserContext userContext) {

		try {
			// WebApplicationContext webApplicationContext =
			// ContextLoader.getCurrentWebApplicationContext();

			Class serviceClass = Class.forName("com.jilefojing.service.I" + itemType.getItemClass().getSimpleName() + "Service");
			Object interfaceImpl = applicationContext.getBean(serviceClass);

			List<BaseEnhanced> enhancedItems = null;
			for (Method _Method : serviceClass.getMethods()) {
				if (_Method.getName().equals("getEnhanceds")) {
					// enhancedItems = (List<BaseEnhanced>)
					// _Method.invoke(webApplicationContext.getBean(serviceClass),
					// itemIds, userContext);

					enhancedItems = (List<BaseEnhanced>) _Method.invoke(interfaceImpl, itemIds, userContext);
					break;
				}
			}

			return enhancedItems;
		} catch (ClassNotFoundException | BeansException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			_LOGGER.error("获取资源Service出错", e1);
		}
		return null;

	}

	@Override
	public List<BaseEnhanced> getEnhancedItemsNotAppend(ItemTypeEnumForSource itemType, List<Long> itemIds, UserContext userContext) {

		try {
			// WebApplicationContext webApplicationContext = ContextLoader
			// .getCurrentWebApplicationContext();

			Class serviceClass = Class.forName("com.jilefojing.service.I" + itemType.getItemClass().getSimpleName() + "Service");
			Object interfaceImpl = applicationContext.getBean(serviceClass);

			List<BaseEnhanced> enhancedItems = null;
			for (Method _Method : serviceClass.getMethods()) {
				if (_Method.getName().equals("getEnhancedsNotAppend")) {
					// enhancedItems = (List<BaseEnhanced>)
					// _Method.invoke(webApplicationContext.getBean(serviceClass),
					// itemIds, userContext);
					enhancedItems = (List<BaseEnhanced>) _Method.invoke(interfaceImpl, itemIds, userContext);
					break;
				}
			}

			return enhancedItems;
		} catch (ClassNotFoundException | BeansException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			_LOGGER.error("获取资源Service出错", e1);
		}
		return null;

	}

	@Override
	public BaseEnhanced appendEnhancedDCategories2EnhancedItem(BaseEnhanced enhancedItem, UserContext userContext) {
		this.appendEnhancedDCategories2EnhancedItems(Arrays.asList(new BaseEnhanced[] { enhancedItem }), userContext);
		return enhancedItem;
	}

	@Override
	public List<BaseEnhanced> appendEnhancedDCategories2EnhancedItems(List<BaseEnhanced> enhancedItems, UserContext userContext) {
		Map<Class<? extends BaseEnhanced>, List<BaseEnhanced>> enhancedMaps = new HashMap<Class<? extends BaseEnhanced>, List<BaseEnhanced>>();
		for (BaseEnhanced _BaseEnhanced : enhancedItems) {
			if (!Detect.notEmpty(enhancedMaps.get(_BaseEnhanced.getClass()))) {
				enhancedMaps.put(_BaseEnhanced.getClass(), new ArrayList<BaseEnhanced>());
			}
			enhancedMaps.get(_BaseEnhanced.getClass()).add(_BaseEnhanced);
		}

		Set<Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>>> enhancedEntrys = enhancedMaps.entrySet();
		for (Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>> _enhancedEntry : enhancedEntrys) {
			this.appendEnhancedDCategories2EnhancedItems(_enhancedEntry.getKey(), _enhancedEntry.getValue(), userContext);
		}
		return enhancedItems;
	}

	@Override
	public BaseEnhanced appendEnhancedTags2EnhancedItem(BaseEnhanced enhancedItem, UserContext userContext) {
		this.appendEnhancedTags2EnhancedItems(Arrays.asList(new BaseEnhanced[] { enhancedItem }), userContext);
		return enhancedItem;
	}

	@Override
	public List<BaseEnhanced> appendEnhancedTags2EnhancedItems(List<BaseEnhanced> enhancedItems, UserContext userContext) {
		Map<Class<? extends BaseEnhanced>, List<BaseEnhanced>> enhancedMaps = new HashMap<Class<? extends BaseEnhanced>, List<BaseEnhanced>>();
		for (BaseEnhanced _BaseEnhanced : enhancedItems) {
			if (!Detect.notEmpty(enhancedMaps.get(_BaseEnhanced.getClass()))) {
				enhancedMaps.put(_BaseEnhanced.getClass(), new ArrayList<BaseEnhanced>());
			}
			enhancedMaps.get(_BaseEnhanced.getClass()).add(_BaseEnhanced);
		}

		Set<Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>>> enhancedEntrys = enhancedMaps.entrySet();
		for (Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>> _enhancedEntry : enhancedEntrys) {
			this.appendEnhancedTags2EnhancedItems(_enhancedEntry.getKey(), _enhancedEntry.getValue(), userContext);
		}
		return enhancedItems;
	}

	private void appendEnhancedTags2EnhancedItems(Class<? extends BaseEnhanced> clazz, List<BaseEnhanced> enhancedItems, UserContext userContext) {
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByEnhancedItemClass(clazz);
		List<Long> itemIds = new ArrayList<Long>();
		for (BaseEnhanced _EnhancedItem : enhancedItems) {
			itemIds.add(_EnhancedItem.getDynamicId());
		}

		List<EnhancedRTagSource> enhancedRTagSources = rTagSourceService.getEnhancedRTagSourcesByItems(itemType, itemIds, userContext);
		if (!Detect.notEmpty(enhancedRTagSources)) {
			return;
		}
		rTagSourceService.appendEnhancedTags2EnhancedRTagSources(enhancedRTagSources, userContext);

		for (BaseEnhanced _EnhancedItem : enhancedItems) {
			this.appendEnhancedTags2EnhancedItem(_EnhancedItem, enhancedRTagSources, userContext);
		}
	}

	private void appendEnhancedTags2EnhancedItem(BaseEnhanced enhancedItem, List<EnhancedRTagSource> enhancedRTagSources, UserContext userContext) {
		List<EnhancedTag> curEnhancedTags = new ArrayList<EnhancedTag>();
		long entityId = enhancedItem.getDynamicId();
		for (EnhancedRTagSource _EnhancedRTagSource : enhancedRTagSources) {
			if (entityId == _EnhancedRTagSource.getEnhancedItem().getDynamicId()) {
				curEnhancedTags.add(_EnhancedRTagSource.getEnhancedTag());
			}
		}
		enhancedItem.set("setEnhancedTags", curEnhancedTags);
	}

	@Override
	public BaseEnhanced appendEnhancedChildren2EnhancedItem(BaseEnhanced enhancedItem, UserContext userContext) {
		this.appendEnhancedChildren2EnhancedItems(Arrays.asList(new BaseEnhanced[] { enhancedItem }), userContext);
		return enhancedItem;
	}

	@Override
	public List<BaseEnhanced> appendEnhancedChildren2EnhancedItems(List<BaseEnhanced> enhancedItems, UserContext userContext) {
		Map<Class<? extends BaseEnhanced>, List<BaseEnhanced>> enhancedMaps = new HashMap<Class<? extends BaseEnhanced>, List<BaseEnhanced>>();
		for (BaseEnhanced _BaseEnhanced : enhancedItems) {
			if (!Detect.notEmpty(enhancedMaps.get(_BaseEnhanced.getClass()))) {
				enhancedMaps.put(_BaseEnhanced.getClass(), new ArrayList<BaseEnhanced>());
			}
			enhancedMaps.get(_BaseEnhanced.getClass()).add(_BaseEnhanced);
		}

		Set<Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>>> enhancedEntrys = enhancedMaps.entrySet();
		for (Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>> _enhancedEntry : enhancedEntrys) {
			this.appendEnhancedChildren2EnhancedItems(_enhancedEntry.getKey(), _enhancedEntry.getValue(), userContext);
		}
		return enhancedItems;
	}

	private void appendEnhancedChildren2EnhancedItems(Class<? extends BaseEnhanced> clazz, List<BaseEnhanced> enhancedItems, UserContext userContext) {
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByEnhancedItemClass(clazz);

		try {
			// WebApplicationContext webApplicationContext =
			// ContextLoader.getCurrentWebApplicationContext();

			Class serviceClass = Class.forName("com.jilefojing.service.I" + itemType.getItemClass().getSimpleName() + "Service");
			Object interfaceImpl = applicationContext.getBean(serviceClass);

			for (Method _Method : serviceClass.getMethods()) {
				if (_Method.getName().equals("appendEnhancedChildren")) {
					Class<?>[] parameterTypeClasses = _Method.getParameterTypes();
					if (Detect.notEmpty(parameterTypeClasses) && parameterTypeClasses.length == 2 && parameterTypeClasses[0] == List.class) {
						// _Method.invoke(webApplicationContext.getBean(serviceClass),
						// enhancedItems, userContext);
						_Method.invoke(interfaceImpl, enhancedItems, userContext);
						break;
					}
				}
			}

		} catch (ClassNotFoundException | BeansException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			_LOGGER.error("获取资源Service出错", e1);
		}

	}

	/**
	 * 
	 * @param clazz
	 * @param enhancedItems
	 *            --->里面的对象必须是clazz类型的
	 * @param userContext
	 */
	private void appendEnhancedDCategories2EnhancedItems(Class<? extends BaseEnhanced> clazz, List<BaseEnhanced> enhancedItems, UserContext userContext) {
		List<Long> itemIds = new ArrayList<Long>();
		for (BaseEnhanced _EnhancedItem : enhancedItems) {
			itemIds.add(_EnhancedItem.getDynamicId());
		}

		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByEnhancedItemClass(clazz);

		Pagination<EnhancedRCategorySource> pagination = new Pagination<EnhancedRCategorySource>(1, Integer.MAX_VALUE, false);
		RCategorySourceQuery rCategorySourceQuery = new RCategorySourceQuery();
		rCategorySourceQuery.setItemIds(itemIds);
		rCategorySourceQuery.setItemTypes(Arrays.asList(new ItemTypeEnumForSource[] { itemType }));

		pagination = rCategorySourceService.getPagination(rCategorySourceQuery, pagination, userContext);
		if (!Detect.notEmpty(pagination.getItems())) {
			return;
		}

		for (BaseEnhanced _EnhancedItem : enhancedItems) {

			List<Long> c1DCategoryIds = new ArrayList<Long>();
			List<Long> c2DCategoryIds = new ArrayList<Long>();
			List<Long> c3DCategoryIds = new ArrayList<Long>();
			List<Long> c4DCategoryIds = new ArrayList<Long>();
			List<Long> c5DCategoryIds = new ArrayList<Long>();
			List<Long> c6DCategoryIds = new ArrayList<Long>();
			List<Long> c7DCategoryIds = new ArrayList<Long>();
			List<Long> c8DCategoryIds = new ArrayList<Long>();
			List<Long> c9DCategoryIds = new ArrayList<Long>();
			List<Long> c10DCategoryIds = new ArrayList<Long>();
			List<Long> c11DCategoryIds = new ArrayList<Long>();
			List<Long> c12DCategoryIds = new ArrayList<Long>();
			List<Long> c13DCategoryIds = new ArrayList<Long>();
			List<Long> c14DCategoryIds = new ArrayList<Long>();
			List<Long> c15DCategoryIds = new ArrayList<Long>();
			List<Long> c16DCategoryIds = new ArrayList<Long>();
			List<Long> c17DCategoryIds = new ArrayList<Long>();
			List<Long> c18DCategoryIds = new ArrayList<Long>();
			List<Long> c19DCategoryIds = new ArrayList<Long>();
			List<Long> c20DCategoryIds = new ArrayList<Long>();
			List<Long> c21DCategoryIds = new ArrayList<Long>();
			List<Long> c22DCategoryIds = new ArrayList<Long>();
			List<Long> c23DCategoryIds = new ArrayList<Long>();
			List<Long> c24DCategoryIds = new ArrayList<Long>();
			List<Long> c25DCategoryIds = new ArrayList<Long>();
			List<Long> c26DCategoryIds = new ArrayList<Long>();
			List<Long> c27DCategoryIds = new ArrayList<Long>();
			List<Long> c28DCategoryIds = new ArrayList<Long>();
			List<Long> c29DCategoryIds = new ArrayList<Long>();
			List<Long> c30DCategoryIds = new ArrayList<Long>();
			List<Long> c31DCategoryIds = new ArrayList<Long>();
			List<Long> c32DCategoryIds = new ArrayList<Long>();
			List<Long> c33DCategoryIds = new ArrayList<Long>();
			// TODO 可以继续增加 资源类别

			List<EnhancedDCategory> enhancedC1DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC2DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC3DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC4DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC5DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC6DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC7DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC8DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC9DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC10DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC11DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC12DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC13DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC14DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC15DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC16DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC17DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC18DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC19DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC20DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC21DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC22DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC23DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC24DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC25DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC26DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC27DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC28DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC29DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC30DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC31DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC32DCategories = new ArrayList<EnhancedDCategory>();
			List<EnhancedDCategory> enhancedC33DCategories = new ArrayList<EnhancedDCategory>();
			// TODO 可以继续增加 资源类别

			for (EnhancedRCategorySource _EnhancedRCategorySource : pagination.getItems()) {
				if (_EnhancedItem.getDynamicId() == _EnhancedRCategorySource.getEnhancedItem().getDynamicId()) {
					if (!c1DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC1Category().getId())) {
						c1DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC1Category().getId());
						enhancedC1DCategories.add(_EnhancedRCategorySource.getEnhancedC1Category());
					}
					if (!c2DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC2Category().getId())) {
						c2DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC2Category().getId());
						enhancedC2DCategories.add(_EnhancedRCategorySource.getEnhancedC2Category());
					}
					if (!c3DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC3Category().getId())) {
						c3DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC3Category().getId());
						enhancedC3DCategories.add(_EnhancedRCategorySource.getEnhancedC3Category());
					}
					if (!c4DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC4Category().getId())) {
						c4DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC4Category().getId());
						enhancedC4DCategories.add(_EnhancedRCategorySource.getEnhancedC4Category());
					}
					if (!c5DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC5Category().getId())) {
						c5DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC5Category().getId());
						enhancedC5DCategories.add(_EnhancedRCategorySource.getEnhancedC5Category());
					}
					if (!c6DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC6Category().getId())) {
						c6DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC6Category().getId());
						enhancedC6DCategories.add(_EnhancedRCategorySource.getEnhancedC6Category());
					}
					if (!c7DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC7Category().getId())) {
						c7DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC7Category().getId());
						enhancedC7DCategories.add(_EnhancedRCategorySource.getEnhancedC7Category());
					}
					if (!c8DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC8Category().getId())) {
						c8DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC8Category().getId());
						enhancedC8DCategories.add(_EnhancedRCategorySource.getEnhancedC8Category());
					}
					if (!c9DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC9Category().getId())) {
						c9DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC9Category().getId());
						enhancedC9DCategories.add(_EnhancedRCategorySource.getEnhancedC9Category());
					}
					if (!c10DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC10Category().getId())) {
						c10DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC10Category().getId());
						enhancedC10DCategories.add(_EnhancedRCategorySource.getEnhancedC10Category());
					}
					if (!c11DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC11Category().getId())) {
						c11DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC11Category().getId());
						enhancedC11DCategories.add(_EnhancedRCategorySource.getEnhancedC11Category());
					}
					if (!c12DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC12Category().getId())) {
						c12DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC12Category().getId());
						enhancedC12DCategories.add(_EnhancedRCategorySource.getEnhancedC12Category());
					}
					if (!c13DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC13Category().getId())) {
						c13DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC13Category().getId());
						enhancedC13DCategories.add(_EnhancedRCategorySource.getEnhancedC13Category());
					}
					if (!c14DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC14Category().getId())) {
						c14DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC14Category().getId());
						enhancedC14DCategories.add(_EnhancedRCategorySource.getEnhancedC14Category());
					}
					if (!c15DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC15Category().getId())) {
						c15DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC15Category().getId());
						enhancedC15DCategories.add(_EnhancedRCategorySource.getEnhancedC15Category());
					}
					if (!c16DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC16Category().getId())) {
						c16DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC16Category().getId());
						enhancedC16DCategories.add(_EnhancedRCategorySource.getEnhancedC16Category());
					}
					if (!c17DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC17Category().getId())) {
						c17DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC17Category().getId());
						enhancedC17DCategories.add(_EnhancedRCategorySource.getEnhancedC17Category());
					}
					if (!c18DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC18Category().getId())) {
						c18DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC18Category().getId());
						enhancedC18DCategories.add(_EnhancedRCategorySource.getEnhancedC18Category());
					}
					if (!c19DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC19Category().getId())) {
						c19DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC19Category().getId());
						enhancedC19DCategories.add(_EnhancedRCategorySource.getEnhancedC19Category());
					}
					if (!c20DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC20Category().getId())) {
						c20DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC20Category().getId());
						enhancedC20DCategories.add(_EnhancedRCategorySource.getEnhancedC20Category());
					}
					if (!c21DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC21Category().getId())) {
						c21DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC21Category().getId());
						enhancedC21DCategories.add(_EnhancedRCategorySource.getEnhancedC21Category());
					}
					if (!c22DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC22Category().getId())) {
						c22DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC22Category().getId());
						enhancedC22DCategories.add(_EnhancedRCategorySource.getEnhancedC22Category());
					}
					if (!c23DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC23Category().getId())) {
						c23DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC23Category().getId());
						enhancedC23DCategories.add(_EnhancedRCategorySource.getEnhancedC23Category());
					}
					if (!c24DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC24Category().getId())) {
						c24DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC24Category().getId());
						enhancedC24DCategories.add(_EnhancedRCategorySource.getEnhancedC24Category());
					}
					if (!c25DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC25Category().getId())) {
						c25DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC25Category().getId());
						enhancedC25DCategories.add(_EnhancedRCategorySource.getEnhancedC25Category());
					}
					if (!c26DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC26Category().getId())) {
						c26DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC26Category().getId());
						enhancedC26DCategories.add(_EnhancedRCategorySource.getEnhancedC26Category());
					}
					if (!c27DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC27Category().getId())) {
						c27DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC27Category().getId());
						enhancedC27DCategories.add(_EnhancedRCategorySource.getEnhancedC27Category());
					}
					if (!c28DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC28Category().getId())) {
						c28DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC28Category().getId());
						enhancedC28DCategories.add(_EnhancedRCategorySource.getEnhancedC28Category());
					}
					if (!c29DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC29Category().getId())) {
						c29DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC29Category().getId());
						enhancedC29DCategories.add(_EnhancedRCategorySource.getEnhancedC29Category());
					}
					if (!c30DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC30Category().getId())) {
						c30DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC30Category().getId());
						enhancedC30DCategories.add(_EnhancedRCategorySource.getEnhancedC30Category());
					}
					if (!c31DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC31Category().getId())) {
						c31DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC31Category().getId());
						enhancedC31DCategories.add(_EnhancedRCategorySource.getEnhancedC31Category());
					}
					if (!c32DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC32Category().getId())) {
						c32DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC32Category().getId());
						enhancedC32DCategories.add(_EnhancedRCategorySource.getEnhancedC32Category());
					}
					if (!c33DCategoryIds.contains(_EnhancedRCategorySource.getEnhancedC33Category().getId())) {
						c33DCategoryIds.add(_EnhancedRCategorySource.getEnhancedC33Category().getId());
						enhancedC33DCategories.add(_EnhancedRCategorySource.getEnhancedC33Category());
					}

					// TODO 可以继续增加 资源类别
				}
			}

			_EnhancedItem.set("setEnhancedC1DCategories", enhancedC1DCategories);
			_EnhancedItem.set("setEnhancedC2DCategories", enhancedC2DCategories);
			_EnhancedItem.set("setEnhancedC3DCategories", enhancedC3DCategories);
			_EnhancedItem.set("setEnhancedC4DCategories", enhancedC4DCategories);
			_EnhancedItem.set("setEnhancedC5DCategories", enhancedC5DCategories);
			_EnhancedItem.set("setEnhancedC6DCategories", enhancedC6DCategories);
			_EnhancedItem.set("setEnhancedC7DCategories", enhancedC7DCategories);
			_EnhancedItem.set("setEnhancedC8DCategories", enhancedC8DCategories);
			_EnhancedItem.set("setEnhancedC9DCategories", enhancedC9DCategories);
			_EnhancedItem.set("setEnhancedC10DCategories", enhancedC10DCategories);
			_EnhancedItem.set("setEnhancedC11DCategories", enhancedC11DCategories);
			_EnhancedItem.set("setEnhancedC12DCategories", enhancedC12DCategories);
			_EnhancedItem.set("setEnhancedC13DCategories", enhancedC13DCategories);
			_EnhancedItem.set("setEnhancedC14DCategories", enhancedC14DCategories);
			_EnhancedItem.set("setEnhancedC15DCategories", enhancedC15DCategories);
			_EnhancedItem.set("setEnhancedC16DCategories", enhancedC16DCategories);
			_EnhancedItem.set("setEnhancedC17DCategories", enhancedC17DCategories);
			_EnhancedItem.set("setEnhancedC18DCategories", enhancedC18DCategories);
			_EnhancedItem.set("setEnhancedC19DCategories", enhancedC19DCategories);
			_EnhancedItem.set("setEnhancedC20DCategories", enhancedC20DCategories);
			_EnhancedItem.set("setEnhancedC21DCategories", enhancedC21DCategories);
			_EnhancedItem.set("setEnhancedC22DCategories", enhancedC22DCategories);
			_EnhancedItem.set("setEnhancedC23DCategories", enhancedC23DCategories);
			_EnhancedItem.set("setEnhancedC24DCategories", enhancedC24DCategories);
			_EnhancedItem.set("setEnhancedC25DCategories", enhancedC25DCategories);
			_EnhancedItem.set("setEnhancedC26DCategories", enhancedC26DCategories);
			_EnhancedItem.set("setEnhancedC27DCategories", enhancedC27DCategories);
			_EnhancedItem.set("setEnhancedC28DCategories", enhancedC28DCategories);
			_EnhancedItem.set("setEnhancedC29DCategories", enhancedC29DCategories);
			_EnhancedItem.set("setEnhancedC30DCategories", enhancedC30DCategories);
			_EnhancedItem.set("setEnhancedC31DCategories", enhancedC31DCategories);
			_EnhancedItem.set("setEnhancedC32DCategories", enhancedC32DCategories);
			_EnhancedItem.set("setEnhancedC33DCategories", enhancedC33DCategories);
			// TODO 可以继续增加 资源类别
		}
	}

	@Override
	public List<RTagSource> addTags(ItemTypeEnumForSource itemType, long itemId, List<Long> tagIds, UserContext userContext) throws UserContextException {
		List<Tag> dbAllTags = tagDao.getListByIds(tagIds);

		Set<Long> tagSetIds = new HashSet<Long>();
		tagSetIds.addAll(tagIds);

		if (!Detect.notEmpty(dbAllTags) || dbAllTags.size() != tagSetIds.size()) {// 判断标签是否存在
			throw new UserContextException("[其中有未存在标签]");
		}

		BaseEnhanced enhancedItem = this.getEnhancedItem(itemType, itemId, userContext);
		rTagSourceDao.deleteByItem(itemType, itemId);// 删除已存在的标签

		StatusEnumForPublish status = (StatusEnumForPublish) enhancedItem.get("getStatus");
		ProgressStatusForSource progressStatus = (ProgressStatusForSource) enhancedItem.get("getProgressStatus");

		Date now = new Date();
		List<RTagSource> rTagSources = new ArrayList<RTagSource>();
		for (long tagId : tagIds) {
			RTagSource _RTagSource = new RTagSource();
			_RTagSource.setItemType(itemType.getCode());
			_RTagSource.setItemId(itemId);
			_RTagSource.setTagId(tagId);
			_RTagSource.setCreateUser(userContext.getUserId());
			_RTagSource.setUpdateTime(now);
			_RTagSource.setCreateTime(now);
			_RTagSource.setStatus(status.getCode());
			_RTagSource.setSort(0D);
			_RTagSource.setProgressStatus(progressStatus.getCode());

			rTagSources.add(_RTagSource);
		}

		return rTagSourceDao.saveList(rTagSources);
	}

	@Override
	public List<RCategorySource> addDCategories(ItemTypeEnumForSource itemType, long itemId, String ckey, List<Long> categoryIds, UserContext userContext) throws UserContextException {
		BaseEnhanced enhancedItem = this.getEnhancedItem(itemType, itemId, userContext);
		// this.appendEnhancedDCategories2EnhancedItem(enhancedItem,
		// userContext);

		// String ckeyFirUp = ckey.substring(0, 1).toUpperCase() +
		// ckey.substring(1);

		List<DCategory> dCategories = dCategoryDao.getListByIds(categoryIds);

		Set<Long> categorySetIds = new HashSet<Long>();
		categorySetIds.addAll(categoryIds);

		if (!Detect.notEmpty(dCategories) || dCategories.size() != categorySetIds.size()) {// 判断类别是否存在
			throw new UserContextException("[其中有未存在的类别]");
		}
		for (DCategory _DCategory : dCategories) {
			if (!_DCategory.getCkey().equals(ckey)) {
				throw new UserContextException("[其中有未存在于(" + ckey + ")中的类别]");
			}
			if (_DCategory.getEntity() == null || _DCategory.getEntity() != 1) {
				throw new UserContextException("[必须设置实体类别]");
			}
		}

		List<RCategorySource> dbRCategorySources = rCategorySourceDao.getListByItem(itemType, itemId);

		Date now = new Date();

		Set<Long> c1Ids = new HashSet<Long>();
		Set<Long> c2Ids = new HashSet<Long>();
		Set<Long> c3Ids = new HashSet<Long>();
		Set<Long> c4Ids = new HashSet<Long>();
		Set<Long> c5Ids = new HashSet<Long>();
		Set<Long> c6Ids = new HashSet<Long>();
		Set<Long> c7Ids = new HashSet<Long>();
		Set<Long> c8Ids = new HashSet<Long>();
		Set<Long> c9Ids = new HashSet<Long>();
		Set<Long> c10Ids = new HashSet<Long>();
		Set<Long> c11Ids = new HashSet<Long>();
		Set<Long> c12Ids = new HashSet<Long>();
		Set<Long> c13Ids = new HashSet<Long>();
		Set<Long> c14Ids = new HashSet<Long>();
		Set<Long> c15Ids = new HashSet<Long>();
		Set<Long> c16Ids = new HashSet<Long>();
		Set<Long> c17Ids = new HashSet<Long>();
		Set<Long> c18Ids = new HashSet<Long>();
		Set<Long> c19Ids = new HashSet<Long>();
		Set<Long> c20Ids = new HashSet<Long>();
		Set<Long> c21Ids = new HashSet<Long>();
		Set<Long> c22Ids = new HashSet<Long>();
		Set<Long> c23Ids = new HashSet<Long>();
		Set<Long> c24Ids = new HashSet<Long>();
		Set<Long> c25Ids = new HashSet<Long>();
		Set<Long> c26Ids = new HashSet<Long>();
		Set<Long> c27Ids = new HashSet<Long>();
		Set<Long> c28Ids = new HashSet<Long>();
		Set<Long> c29Ids = new HashSet<Long>();
		Set<Long> c30Ids = new HashSet<Long>();
		Set<Long> c31Ids = new HashSet<Long>();
		Set<Long> c32Ids = new HashSet<Long>();
		Set<Long> c33Ids = new HashSet<Long>();
		// TODO 可以继续增加 资源类别

		double sort = 0D;// 设置排序

		if (Detect.notEmpty(dbRCategorySources)) {// 去除已存在的设置类别配置 并删除数据库关系配置
			List<Long> _RCategorySourceIds = new ArrayList<Long>();

			sort = dbRCategorySources.get(0).getSort() == null ? 0D : dbRCategorySources.get(0).getSort();

			for (RCategorySource _RCategorySource : dbRCategorySources) {
				c1Ids.add(_RCategorySource.getCategoryC1() == null ? 0 : _RCategorySource.getCategoryC1());
				c2Ids.add(_RCategorySource.getCategoryC2() == null ? 0 : _RCategorySource.getCategoryC2());
				c3Ids.add(_RCategorySource.getCategoryC3() == null ? 0 : _RCategorySource.getCategoryC3());
				c4Ids.add(_RCategorySource.getCategoryC4() == null ? 0 : _RCategorySource.getCategoryC4());
				c5Ids.add(_RCategorySource.getCategoryC5() == null ? 0 : _RCategorySource.getCategoryC5());
				c6Ids.add(_RCategorySource.getCategoryC6() == null ? 0 : _RCategorySource.getCategoryC6());
				c7Ids.add(_RCategorySource.getCategoryC7() == null ? 0 : _RCategorySource.getCategoryC7());
				c8Ids.add(_RCategorySource.getCategoryC8() == null ? 0 : _RCategorySource.getCategoryC8());
				c9Ids.add(_RCategorySource.getCategoryC9() == null ? 0 : _RCategorySource.getCategoryC9());
				c10Ids.add(_RCategorySource.getCategoryC10() == null ? 0 : _RCategorySource.getCategoryC10());
				c11Ids.add(_RCategorySource.getCategoryC11() == null ? 0 : _RCategorySource.getCategoryC11());
				c12Ids.add(_RCategorySource.getCategoryC12() == null ? 0 : _RCategorySource.getCategoryC12());
				c13Ids.add(_RCategorySource.getCategoryC13() == null ? 0 : _RCategorySource.getCategoryC13());
				c14Ids.add(_RCategorySource.getCategoryC14() == null ? 0 : _RCategorySource.getCategoryC14());
				c15Ids.add(_RCategorySource.getCategoryC15() == null ? 0 : _RCategorySource.getCategoryC15());
				c16Ids.add(_RCategorySource.getCategoryC16() == null ? 0 : _RCategorySource.getCategoryC16());
				c17Ids.add(_RCategorySource.getCategoryC17() == null ? 0 : _RCategorySource.getCategoryC17());
				c18Ids.add(_RCategorySource.getCategoryC18() == null ? 0 : _RCategorySource.getCategoryC18());
				c19Ids.add(_RCategorySource.getCategoryC19() == null ? 0 : _RCategorySource.getCategoryC19());
				c20Ids.add(_RCategorySource.getCategoryC20() == null ? 0 : _RCategorySource.getCategoryC20());
				c21Ids.add(_RCategorySource.getCategoryC21() == null ? 0 : _RCategorySource.getCategoryC21());
				c22Ids.add(_RCategorySource.getCategoryC22() == null ? 0 : _RCategorySource.getCategoryC22());
				c23Ids.add(_RCategorySource.getCategoryC23() == null ? 0 : _RCategorySource.getCategoryC23());
				c24Ids.add(_RCategorySource.getCategoryC24() == null ? 0 : _RCategorySource.getCategoryC24());
				c25Ids.add(_RCategorySource.getCategoryC25() == null ? 0 : _RCategorySource.getCategoryC25());
				c26Ids.add(_RCategorySource.getCategoryC26() == null ? 0 : _RCategorySource.getCategoryC26());
				c27Ids.add(_RCategorySource.getCategoryC27() == null ? 0 : _RCategorySource.getCategoryC27());
				c28Ids.add(_RCategorySource.getCategoryC28() == null ? 0 : _RCategorySource.getCategoryC28());
				c29Ids.add(_RCategorySource.getCategoryC29() == null ? 0 : _RCategorySource.getCategoryC29());
				c30Ids.add(_RCategorySource.getCategoryC30() == null ? 0 : _RCategorySource.getCategoryC30());
				c31Ids.add(_RCategorySource.getCategoryC31() == null ? 0 : _RCategorySource.getCategoryC31());
				c32Ids.add(_RCategorySource.getCategoryC32() == null ? 0 : _RCategorySource.getCategoryC32());
				c33Ids.add(_RCategorySource.getCategoryC33() == null ? 0 : _RCategorySource.getCategoryC33());
				// TODO 可以继续增加 资源类别

				_RCategorySourceIds.add(_RCategorySource.getId());
			}

			rCategorySourceDao.deleteByIds(_RCategorySourceIds, RCategorySource.FIELD_ID, RCategorySource.class);
		} else {
			if (!ckey.equals("c1")) {
				c1Ids.add(0L);
			}
			if (!ckey.equals("c2")) {
				c2Ids.add(0L);
			}
			if (!ckey.equals("c3")) {
				c3Ids.add(0L);
			}
			if (!ckey.equals("c4")) {
				c4Ids.add(0L);
			}
			if (!ckey.equals("c5")) {
				c5Ids.add(0L);
			}
			if (!ckey.equals("c6")) {
				c6Ids.add(0L);
			}
			if (!ckey.equals("c7")) {
				c7Ids.add(0L);
			}
			if (!ckey.equals("c8")) {
				c8Ids.add(0L);
			}
			if (!ckey.equals("c9")) {
				c9Ids.add(0L);
			}
			if (!ckey.equals("c10")) {
				c10Ids.add(0L);
			}
			if (!ckey.equals("c11")) {
				c11Ids.add(0L);
			}
			if (!ckey.equals("c12")) {
				c12Ids.add(0L);
			}
			if (!ckey.equals("c13")) {
				c13Ids.add(0L);
			}
			if (!ckey.equals("c14")) {
				c14Ids.add(0L);
			}
			if (!ckey.equals("c15")) {
				c15Ids.add(0L);
			}
			if (!ckey.equals("c16")) {
				c16Ids.add(0L);
			}
			if (!ckey.equals("c17")) {
				c17Ids.add(0L);
			}
			if (!ckey.equals("c18")) {
				c18Ids.add(0L);
			}
			if (!ckey.equals("c19")) {
				c19Ids.add(0L);
			}
			if (!ckey.equals("c20")) {
				c20Ids.add(0L);
			}
			if (!ckey.equals("c21")) {
				c21Ids.add(0L);
			}
			if (!ckey.equals("c22")) {
				c22Ids.add(0L);
			}
			if (!ckey.equals("c23")) {
				c23Ids.add(0L);
			}
			if (!ckey.equals("c24")) {
				c24Ids.add(0L);
			}
			if (!ckey.equals("c25")) {
				c25Ids.add(0L);
			}
			if (!ckey.equals("c26")) {
				c26Ids.add(0L);
			}
			if (!ckey.equals("c27")) {
				c27Ids.add(0L);
			}
			if (!ckey.equals("c28")) {
				c28Ids.add(0L);
			}
			if (!ckey.equals("c29")) {
				c29Ids.add(0L);
			}
			if (!ckey.equals("c30")) {
				c30Ids.add(0L);
			}
			if (!ckey.equals("c31")) {
				c31Ids.add(0L);
			}
			if (!ckey.equals("c32")) {
				c32Ids.add(0L);
			}
			if (!ckey.equals("c33")) {
				c33Ids.add(0L);
			}
			// if // TODO 可以继续增加 资源类别
		}

		if (ckey.equals("c1")) {
			c1Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c2")) {
			c2Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c3")) {
			c3Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c4")) {
			c4Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c5")) {
			c5Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c6")) {
			c6Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c7")) {
			c7Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c8")) {
			c8Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c9")) {
			c9Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c10")) {
			c10Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c11")) {
			c11Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c12")) {
			c12Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c13")) {
			c13Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c14")) {
			c14Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c15")) {
			c15Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c16")) {
			c16Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c17")) {
			c17Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c18")) {
			c18Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c19")) {
			c19Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c20")) {
			c20Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c21")) {
			c21Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c22")) {
			c22Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c23")) {
			c23Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c24")) {
			c24Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c25")) {
			c25Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c26")) {
			c26Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c27")) {
			c27Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c28")) {
			c28Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c29")) {
			c29Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c30")) {
			c30Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c31")) {
			c31Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c32")) {
			c32Ids = new HashSet<Long>(categoryIds);
		} else if (ckey.equals("c33")) {
			c33Ids = new HashSet<Long>(categoryIds);
		}

		// else // TODO 可以继续增加 资源类别

		List<List<Long>> arrs = new ArrayList<List<Long>>();
		arrs.add(new ArrayList<Long>(c1Ids));
		arrs.add(new ArrayList<Long>(c2Ids));
		arrs.add(new ArrayList<Long>(c3Ids));
		arrs.add(new ArrayList<Long>(c4Ids));
		arrs.add(new ArrayList<Long>(c5Ids));
		arrs.add(new ArrayList<Long>(c6Ids));
		arrs.add(new ArrayList<Long>(c7Ids));
		arrs.add(new ArrayList<Long>(c8Ids));
		arrs.add(new ArrayList<Long>(c9Ids));
		arrs.add(new ArrayList<Long>(c10Ids));
		arrs.add(new ArrayList<Long>(c11Ids));
		arrs.add(new ArrayList<Long>(c12Ids));
		arrs.add(new ArrayList<Long>(c13Ids));
		arrs.add(new ArrayList<Long>(c14Ids));
		arrs.add(new ArrayList<Long>(c15Ids));
		arrs.add(new ArrayList<Long>(c16Ids));
		arrs.add(new ArrayList<Long>(c17Ids));
		arrs.add(new ArrayList<Long>(c18Ids));
		arrs.add(new ArrayList<Long>(c19Ids));
		arrs.add(new ArrayList<Long>(c20Ids));
		arrs.add(new ArrayList<Long>(c21Ids));
		arrs.add(new ArrayList<Long>(c22Ids));
		arrs.add(new ArrayList<Long>(c23Ids));
		arrs.add(new ArrayList<Long>(c24Ids));
		arrs.add(new ArrayList<Long>(c25Ids));
		arrs.add(new ArrayList<Long>(c26Ids));
		arrs.add(new ArrayList<Long>(c27Ids));
		arrs.add(new ArrayList<Long>(c28Ids));
		arrs.add(new ArrayList<Long>(c29Ids));
		arrs.add(new ArrayList<Long>(c30Ids));
		arrs.add(new ArrayList<Long>(c31Ids));
		arrs.add(new ArrayList<Long>(c32Ids));
		arrs.add(new ArrayList<Long>(c33Ids));
		// TODO 可以继续增加 资源类别

		arrs = CombinationUtil.getCombinationResult(arrs);

		StatusEnumForPublish status = (StatusEnumForPublish) enhancedItem.get("getStatus");

		ProgressStatusForSource progressStatusForSource = ProgressStatusForSource.SERIALISE;
		if (enhancedItem.getBoolean("isEntity")) {
			BaseEnhanced parentItem = (BaseEnhanced) enhancedItem.get("getEnhancedParent");
			if (parentItem != null && parentItem.getDynamicId() > 0) {
				parentItem = this.getEnhancedItem(itemType, parentItem.getDynamicId(), userContext);
				progressStatusForSource = (ProgressStatusForSource) parentItem.get("getProgressStatus");
				if (progressStatusForSource == null) {
					progressStatusForSource = ProgressStatusForSource.SERIALISE;
				}
			} else {
				progressStatusForSource = (ProgressStatusForSource) enhancedItem.get("getProgressStatus");
				if (progressStatusForSource == null) {
					progressStatusForSource = ProgressStatusForSource.SERIALISE;
				}
			}
		} else {
			progressStatusForSource = (ProgressStatusForSource) enhancedItem.get("getProgressStatus");
			if (progressStatusForSource == null) {
				progressStatusForSource = ProgressStatusForSource.SERIALISE;
			}
		}

		List<RCategorySource> rCategorySources = new ArrayList<RCategorySource>();
		for (List<Long> arr : arrs) {
			RCategorySource _RCategorySource = new RCategorySource();
			_RCategorySource.setItemId(itemId);
			_RCategorySource.setItemType(itemType.getCode());
			_RCategorySource.setCreateUser(userContext.getUserId());
			_RCategorySource.setCreateTime(now);
			_RCategorySource.setSort(sort);
			_RCategorySource.setUpdateTime(now);
			_RCategorySource.setHotSort(0D);

			_RCategorySource.setCategoryC1(arr.get(0));
			_RCategorySource.setCategoryC2(arr.get(1));
			_RCategorySource.setCategoryC3(arr.get(2));
			_RCategorySource.setCategoryC4(arr.get(3));
			_RCategorySource.setCategoryC5(arr.get(4));
			_RCategorySource.setCategoryC6(arr.get(5));
			_RCategorySource.setCategoryC7(arr.get(6));
			_RCategorySource.setCategoryC8(arr.get(7));
			_RCategorySource.setCategoryC9(arr.get(8));
			_RCategorySource.setCategoryC10(arr.get(9));
			_RCategorySource.setCategoryC11(arr.get(10));
			_RCategorySource.setCategoryC12(arr.get(11));
			_RCategorySource.setCategoryC13(arr.get(12));
			_RCategorySource.setCategoryC14(arr.get(13));
			_RCategorySource.setCategoryC15(arr.get(14));
			_RCategorySource.setCategoryC16(arr.get(15));
			_RCategorySource.setCategoryC17(arr.get(16));
			_RCategorySource.setCategoryC18(arr.get(17));
			_RCategorySource.setCategoryC19(arr.get(18));
			_RCategorySource.setCategoryC20(arr.get(19));
			_RCategorySource.setCategoryC21(arr.get(20));
			_RCategorySource.setCategoryC22(arr.get(21));
			_RCategorySource.setCategoryC23(arr.get(22));
			_RCategorySource.setCategoryC24(arr.get(23));
			_RCategorySource.setCategoryC25(arr.get(24));
			_RCategorySource.setCategoryC26(arr.get(25));
			_RCategorySource.setCategoryC27(arr.get(26));
			_RCategorySource.setCategoryC28(arr.get(27));
			_RCategorySource.setCategoryC29(arr.get(28));
			_RCategorySource.setCategoryC30(arr.get(29));
			_RCategorySource.setCategoryC31(arr.get(30));
			_RCategorySource.setCategoryC32(arr.get(31));
			_RCategorySource.setCategoryC33(arr.get(32));
			// TODO 可以继续增加 资源类别

			_RCategorySource.setStatus(status.getCode());
			_RCategorySource.setProgressStatus(progressStatusForSource.getCode());

			rCategorySources.add(_RCategorySource);
		}

		this.rectifyUpdate(rCategorySources);

		return rCategorySourceDao.saveList(rCategorySources);
	}

	/**
	 * 存在的类别列表中有一个全不是0 那么就把是有0类别数据全部除去
	 * 
	 * @param rCategorySources
	 */
	private void rectifyUpdate(List<RCategorySource> rCategorySources) {
		boolean isExist = false;// 是否有全不为0的数据列表
		for (int i = rCategorySources.size() - 1; i > -1; i--) {
			RCategorySource _RCategorySource = rCategorySources.get(i);
			if (_RCategorySource.getCategoryC1() > 0 && _RCategorySource.getCategoryC2() > 0 && _RCategorySource.getCategoryC3() > 0 && _RCategorySource.getCategoryC4() > 0
					&& _RCategorySource.getCategoryC5() > 0 && _RCategorySource.getCategoryC6() > 0 && _RCategorySource.getCategoryC7() > 0 && _RCategorySource.getCategoryC8() > 0
					&& _RCategorySource.getCategoryC9() > 0 && _RCategorySource.getCategoryC10() > 0 && _RCategorySource.getCategoryC11() > 0 && _RCategorySource.getCategoryC12() > 0
					&& _RCategorySource.getCategoryC13() > 0 && _RCategorySource.getCategoryC14() > 0 && _RCategorySource.getCategoryC15() > 0 && _RCategorySource.getCategoryC16() > 0
					&& _RCategorySource.getCategoryC17() > 0 && _RCategorySource.getCategoryC18() > 0 && _RCategorySource.getCategoryC19() > 0 && _RCategorySource.getCategoryC20() > 0
					&& _RCategorySource.getCategoryC21() > 0 && _RCategorySource.getCategoryC22() > 0 && _RCategorySource.getCategoryC23() > 0 && _RCategorySource.getCategoryC24() > 0
					&& _RCategorySource.getCategoryC25() > 0 && _RCategorySource.getCategoryC26() > 0 && _RCategorySource.getCategoryC27() > 0 && _RCategorySource.getCategoryC28() > 0
					&& _RCategorySource.getCategoryC29() > 0 && _RCategorySource.getCategoryC30() > 0 && _RCategorySource.getCategoryC31() > 0 && _RCategorySource.getCategoryC32() > 0
					&& _RCategorySource.getCategoryC33() > 0) {// TODO 可以继续增加
																// 资源类别
				isExist = true;
				break;
			}
		}
		if (isExist) {
			for (int i = rCategorySources.size() - 1; i > -1; i--) {
				RCategorySource _RCategorySource = rCategorySources.get(i);
				if (_RCategorySource.getCategoryC1() == 0 || _RCategorySource.getCategoryC2() == 0 || _RCategorySource.getCategoryC3() == 0 || _RCategorySource.getCategoryC4() == 0
						|| _RCategorySource.getCategoryC5() == 0 || _RCategorySource.getCategoryC6() == 0 || _RCategorySource.getCategoryC7() == 0 || _RCategorySource.getCategoryC8() == 0
						|| _RCategorySource.getCategoryC9() == 0 || _RCategorySource.getCategoryC10() == 0 || _RCategorySource.getCategoryC11() == 0 || _RCategorySource.getCategoryC12() == 0
						|| _RCategorySource.getCategoryC13() == 0 || _RCategorySource.getCategoryC14() == 0 || _RCategorySource.getCategoryC15() == 0 || _RCategorySource.getCategoryC16() == 0
						|| _RCategorySource.getCategoryC17() == 0 || _RCategorySource.getCategoryC18() == 0 || _RCategorySource.getCategoryC19() == 0 || _RCategorySource.getCategoryC20() == 0
						|| _RCategorySource.getCategoryC21() == 0 || _RCategorySource.getCategoryC22() == 0 || _RCategorySource.getCategoryC23() == 0 || _RCategorySource.getCategoryC24() == 0
						|| _RCategorySource.getCategoryC25() == 0 || _RCategorySource.getCategoryC26() == 0 || _RCategorySource.getCategoryC27() == 0 || _RCategorySource.getCategoryC28() == 0
						|| _RCategorySource.getCategoryC29() == 0 || _RCategorySource.getCategoryC30() == 0 || _RCategorySource.getCategoryC31() == 0 || _RCategorySource.getCategoryC32() == 0
						|| _RCategorySource.getCategoryC33() == 0) {// TODO
																	// 可以继续增加
																	// 资源类别
					rCategorySources.remove(i);
				}
			}
		}

	}

	@Override
	public BaseEnhanced appendEnhancedParent2EnhancedItem(BaseEnhanced enhancedItem, UserContext userContext) {
		this.appendEnhancedParent2EnhancedItems(Arrays.asList(new BaseEnhanced[] { enhancedItem }), userContext);
		return enhancedItem;
	}

	@Override
	public List<BaseEnhanced> appendEnhancedParent2EnhancedItems(List<BaseEnhanced> enhancedItems, UserContext userContext) {
		Map<Class<? extends BaseEnhanced>, List<BaseEnhanced>> enhancedMaps = new HashMap<Class<? extends BaseEnhanced>, List<BaseEnhanced>>();
		for (BaseEnhanced _BaseEnhanced : enhancedItems) {
			if (!Detect.notEmpty(enhancedMaps.get(_BaseEnhanced.getClass()))) {
				enhancedMaps.put(_BaseEnhanced.getClass(), new ArrayList<BaseEnhanced>());
			}
			enhancedMaps.get(_BaseEnhanced.getClass()).add(_BaseEnhanced);
		}

		Set<Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>>> enhancedEntrys = enhancedMaps.entrySet();
		for (Entry<Class<? extends BaseEnhanced>, List<BaseEnhanced>> _enhancedEntry : enhancedEntrys) {
			this.appendEnhancedParent2EnhancedItems(_enhancedEntry.getKey(), _enhancedEntry.getValue(), userContext);
		}
		return enhancedItems;
	}

	private void appendEnhancedParent2EnhancedItems(Class<? extends BaseEnhanced> clazz, List<BaseEnhanced> enhancedItems, UserContext userContext) {
		ItemTypeEnumForSource itemType = ItemTypeEnumForSource.getItemTypeEnumByEnhancedItemClass(clazz);

		try {
			// WebApplicationContext webApplicationContext =
			// ContextLoader.getCurrentWebApplicationContext();

			Class serviceClass = Class.forName("com.jilefojing.service.I" + itemType.getItemClass().getSimpleName() + "Service");
			Object interfaceImpl = applicationContext.getBean(serviceClass);

			for (Method _Method : serviceClass.getMethods()) {
				if (_Method.getName().equals("appendEnhancedParent")) {
					Class<?>[] parameterTypeClasses = _Method.getParameterTypes();
					if (Detect.notEmpty(parameterTypeClasses) && parameterTypeClasses.length == 2 && parameterTypeClasses[0] == List.class) {
						// _Method.invoke(webApplicationContext.getBean(serviceClass),
						// enhancedItems, userContext);
						_Method.invoke(interfaceImpl, enhancedItems, userContext);
						break;
					}
				}
			}

		} catch (ClassNotFoundException | BeansException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
			_LOGGER.error("获取资源Service出错", e1);
		}

	}

	@Override
	public Pagination<Source> getPaginationByKeywordsForActive(String keywords, Pagination<Source> pagination, UserContext userContext) {
		pagination = sourceDao.getPaginationByTitleLike(keywords, pagination);
		if (!Detect.notEmpty(pagination.getItems())) {
			return pagination;
		}
		for (Source _Source : pagination.getItems()) {
			if (_Source.getItemType() == ItemTypeEnumForSource.VIDEO.getCode() || _Source.getItemType() == ItemTypeEnumForSource.AUDIO.getCode()) {
				if (Detect.notEmpty(_Source.getCoverUrl())) {
					_Source.setCoverUrl(QiniuUtil.getImagesDomain() + _Source.getCoverUrl());
				}
				if (!Detect.notEmpty(_Source.getCoverUrl()) && _Source.getItemType() == ItemTypeEnumForSource.AUDIO.getCode()) {
					_Source.setCoverUrl("http://oe2fpvujn.bkt.clouddn.com/audio/cover/default.jpg");
				}
			} else if (_Source.getItemType() == ItemTypeEnumForSource.ARTICLE.getCode()) {
				Elements _imgElements = Jsoup.parse(_Source.getInfo()).select("img[src]");
				if (_imgElements.size() > 0) {
					_Source.setCoverUrl(_imgElements.get(0).attr("src").replaceAll("\\$_PREFIX_\\$", QiniuUtil.getImagesDomain()));
				}
			}
		}
		return pagination;
	}
}
